﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Roles : System.Web.UI.Page {

    #region Methods ///////////////////////////////////////////////////////////////////////////////

    /// <summary>
    /// Binds the roles.
    /// </summary>
    private void BindRoles() {

        rptRoles.DataSource = System.Web.Security.Roles.GetAllRoles();
        rptRoles.DataBind();
    }

    /// <summary>
    /// Handles the command.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
    protected void HandleCommand(object sender, CommandEventArgs e) {

        string rolename;
        switch (e.CommandName) {
            case "Create":
                rolename = txtRoleName.Text;
                if (!string.IsNullOrEmpty(rolename)) {
                    // TODO check if roles is not existing already
                    System.Web.Security.Roles.CreateRole(rolename);
                }
                txtRoleName.Text = "";
                this.BindRoles();
                break;
            case "Delete":
                rolename = e.CommandArgument as string;
                if(!string.IsNullOrEmpty(rolename))
                    System.Web.Security.Roles.DeleteRole(rolename);
                this.BindRoles();
                break;
        }
    }

    /// <summary>
    /// Raises the <see cref="E:System.Web.UI.Control.Load"/> event.
    /// </summary>
    /// <param name="e">The <see cref="T:System.EventArgs"/> object that contains the event data.</param>
    protected override void OnLoad(EventArgs e) {
        base.OnLoad(e);

        if (!IsPostBack) 
            this.BindRoles();
    }
    #endregion
}
